﻿package com._public._air 
{
	import com._public._debug.GetErrMsg;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	/**
	 * AIR文件存储
	 * @author WZH(shch8.com)
	 */
	public class WriteFile 
	{
		private static var Instace:WriteFile=new WriteFile;
		public static function getInstance():WriteFile {
			return Instace;
		}
		/**
		 * 2012-11-29 改用类获取方式声明File对象，便于将此类放置在代码工程中
		 */		
		private var FileClass:Class=getDefinitionByName("flash.filesystem.File") as Class;
		private var fileStreamClass:Class=getDefinitionByName("flash.filesystem.FileStream") as Class;
		
		/**
		 * 同步方式读取本地文件
		 * @param	path
		 * @param	txt
		 */
		public function readByte(path:Object):String {
			var file:Object =(path is FileClass)?(path as FileClass):(new FileClass(String(path)))
			if (!file.exists) {
				trace("未找到路径:"+file.url, "文件读取失败");
				return "";
			}
			var fileStream:Object = new fileStreamClass();
			fileStream.open(file, "read");
			var s:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
			fileStream.close();
			return s;
		}
		
		/**
		 * 同步方式读取本地文件
		 * @param	path
		 * @param	txt
		 */
		public function readTxt(path:Object):String {
			
			var file:Object =(path is FileClass)?(path as FileClass):(new FileClass(String(path)))
			if (!file.exists) {
				trace("未找到路径:"+file.url, "文件读取失败");
				return "";
			}
			var fileStream:Object = new fileStreamClass();
			fileStream.open(file, "read");
			var s:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
			fileStream.close();
			return s;
		}
		/**
		 * 文本文式写入，如txt或rar
		 * @param	path
		 * @param	txt
		 */
		public function writeTxt(path:Object,txt:String):Boolean{
			var cFile:Object =(path is FileClass)?(path as FileClass):(new FileClass(String(path)))
			try{
				var fs:Object = new fileStreamClass()
				fs.open(cFile,"write")
				fs.position = 0
				fs.writeUTFBytes(txt)
				fs.close()
				cFile = null;
			}catch (e:*) {
				trace(e, "文件写入出错");
				return false;
				}
			return true;
		}
		
		
		/**
		 * 创建文件夹，如txt或rar
		 * @param	path	路径
		 * @param	txt		文件夹名
		 */
		public function createFolder(path:String,folder:String):String {
		 var file:Object = new FileClass(path + "/" + folder);
		 if (file.exists) return file.nativePath;
		 file = new FileClass(path);
		 if (!file.exists) {
			 return "";
			 }
			try {
				file.nativePath+="/"+folder
				file.createDirectory();
			}catch (e:*) {
				}
			return file.nativePath;
		}
     
	 
		/**
		 * 二进制方式写入文件
		 * @param	path
		 * @param	byteArray
		 * @param	useCompress 是否使用压缩
		 */
		public function writeByte(path:Object,byteArray:ByteArray,useCompress:Boolean=false):void {
			var cFile:Object =(path is FileClass)?(path as FileClass):(new FileClass(String(path)))
				var fs:Object = new fileStreamClass()
				fs.open(cFile, "write");
				fs.position = 0;
				if(useCompress)byteArray.compress()
				fs.writeBytes(byteArray)
				fs.close()
				cFile = null;
		}
		/**
		 * 删除文件
		 * @param	path
		 */
		public function delFile(path:Object):void {
			var cFile:Object =(path is FileClass)?(path as FileClass):(new FileClass(String(path)))
			if (cFile.exists) cFile.deleteFile();
		}
		/**
		 *  文件操作，移动或复制
		 * @param	fromPath	源路径
		 * @param	toPath		目标路径
		 * @param	isCopy		复制或移动
		 * @param	callErr		未找到路径时回调报错
		 * @param	overwrite	如果目标已经存在，是否盖掉
		 */
		public function controlFile(fromPath:String,toPath:String,isCopy:Boolean=true,callErr:Function=null,callComplete:Function=null,overwrite:Boolean=true):void {
			var sourceFile:Object = new FileClass(fromPath)
			var destination:Object = new FileClass(toPath)
			sourceFile.addEventListener(Event.COMPLETE, fileMoveCompleteHandler);
			sourceFile.addEventListener(IOErrorEvent.IO_ERROR, errFun);
			
			if(isCopy){
				sourceFile.copyToAsync(destination, overwrite);
			}else {
				sourceFile.moveToAsync(destination, overwrite);
			}

			function errFun(e:IOErrorEvent):void {
				if(callErr is Function)callErr("文件"+(isCopy?"复制":"移动")+"出错/n从:"+fromPath+"到"+toPath)
			}
			function fileMoveCompleteHandler(event:Event):void {
				sourceFile.removeEventListener(Event.COMPLETE, fileMoveCompleteHandler);
				sourceFile.removeEventListener(IOErrorEvent.IO_ERROR, errFun);
				if(callComplete is Function)callComplete()
			}
		}
	}
	
}